//
//  MULXAuthUtils.m
//  UniLoginSDK
//
//  Created by xiaofeng on 13-12-25.
//  Copyright (c) 2013年 Mafengwo Inc. All rights reserved.
//

#import "MULXAuthUtils.h"
#import "NSString+MULAdditions.h"
#import <CommonCrypto/CommonHMAC.h>

@implementation MULXAuthUtils
    
+ (NSString*)uuidStr;
{
    CFUUIDRef newUniqueID = CFUUIDCreate(kCFAllocatorDefault);
    CFStringRef newUniqueIDString = CFUUIDCreateString(kCFAllocatorDefault, newUniqueID);
    NSString *guid = (__bridge NSString *)newUniqueIDString;
    CFRelease(newUniqueIDString);
    CFRelease(newUniqueID);
    return([guid lowercaseString]);
}
    
+ (NSString*)hmac_sha1:(NSString*)key text:(NSString*)text
{
    const char *cKey  = [key cStringUsingEncoding:NSUTF8StringEncoding];
    const char *cData = [text cStringUsingEncoding:NSUTF8StringEncoding];
    
    char cHMAC[CC_SHA1_DIGEST_LENGTH];
    
    CCHmac(kCCHmacAlgSHA1, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
    
    NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC length:CC_SHA1_DIGEST_LENGTH];
    
    NSString *hash = [NSString base64StringFromData:HMAC length:[HMAC length]];//base64 编码。
    
    return hash;
}
    
+ (NSMutableDictionary*)signatureAuthParams:(NSMutableDictionary*)orignalParams
                                 requestURL:(NSString*)requestURL
                        requestIsPostMethod:(BOOL)bPostOrNot
                        withAuthConsumerKey:(NSString*)consumerKey
                             consumerSecret:(NSString*)consumerSecret
                                accessToken:(NSString*)accessToken
                                tokenSecret:(NSString*)tokenSecret;
{
//    NSAssert([consumerKey length] > 0 , @"consmerKey is empty");
//    NSAssert([consumerSecret length] > 0, @"consumerSecret is empty");
    NSAssert([requestURL length] > 0, @"request url is nil");
    
    if(!orignalParams) {
        orignalParams = [NSMutableDictionary dictionaryWithCapacity:16];
    }
    
//    [orignalParams removeObjectForKey:@"oauth_token"];
//    [orignalParams removeObjectForKey:@"oauth_consumer_key"];
//    [orignalParams removeObjectForKey:@"oauth_signature_method"];
//    [orignalParams removeObjectForKey:@"oauth_timestamp"];
//    [orignalParams removeObjectForKey:@"oauth_nonce"];
//    [orignalParams removeObjectForKey:@"oauth_version"];
//    [orignalParams removeObjectForKey:@"oauth_signature"];
    
//    if ([accessToken length])
//    {
//        [orignalParams setValue:accessToken forKey:@"oauth_token"];
//    }
    
    long long timeStamp = (long long)[[NSDate date] timeIntervalSince1970];
    NSString *nonce = [self uuidStr];
//    NSLog(@">>>> noce:%@", nonce);
    
//    [orignalParams setValue:consumerKey forKey:@"oauth_consumer_key"];
//    [orignalParams setValue:@"HMAC-SHA1" forKey:@"oauth_signature_method"];
//    [orignalParams setValue:[NSString stringWithFormat:@"%lld", timeStamp] forKey:@"oauth_timestamp"];
//    [orignalParams setValue:nonce forKey:@"oauth_nonce"];
//    [orignalParams setValue:@"1.0" forKey:@"oauth_version"];
//    [orignalParams setValue:@"client_auth" forKey:@"x_auth_mode"];
    
    //sort key-value pares using key order
    NSArray *allSortedKeys = [[orignalParams allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
    NSMutableString *sortedQueryStr = [NSMutableString string];
    
    for (NSString *key in allSortedKeys)
    {
        if ([sortedQueryStr length] > 0)
        {
            [sortedQueryStr appendString:@"&"];
        }
        
        NSString *value = [orignalParams objectForKey:key];
        [sortedQueryStr appendFormat:@"%@=%@", [key mul_urlEncode], [value mul_urlEncode]];
    }
    
    //build base string
    NSMutableString *baseString = [NSMutableString string];
    [baseString appendFormat:bPostOrNot ? @"POST" : @"GET"];
    [baseString appendString:@"&"];
    [baseString appendString:[requestURL mul_urlEncode]];
    [baseString appendString:@"&"];
    [baseString appendString:[sortedQueryStr mul_urlEncode]];
    
//    NSLog(@"baseString>> %@", baseString);
    
    //build signature secret string
    if ([tokenSecret length] == 0)
    {
        tokenSecret = @"";
    }
//    NSString *signSecret = [NSString stringWithFormat:@"%@&%@", [consumerSecret mul_urlEncode], [tokenSecret mul_urlEncode]];
    
    
//    NSString *signatureStr = [self hmac_sha1:signSecret text:baseString];
    
//    [orignalParams setValue:signatureStr forKey:@"oauth_signature"];
    
    return orignalParams;
}

@end
